* Replicates the CMPS Analysis in the online appendix for Zingher 2021

clear all
use "CMPS2016.dta"
*Specify working directory here*

//Renaming
rename s2_1 White
rename s2_2 Latino
rename s2_3 Black
rename s2_4 Asian
rename s2_5 Arab
rename s2_6 Native
rename s2_7 Other_Race
replace Other_Race = 1 if Arab==1
replace Other_Race = 1 if Native==1
rename c25 Party_ID
rename c26 Partisanship
rename c39 Immigration
rename c40 Gay_Marriage
rename c41 Imm_Econ
rename c42 Clim_Change
rename c43 Tax_Cuts
rename c44 VID
rename s3 Gender
rename c45 Health_Care
rename s4 State
rename age Age
rename c129 Religion
rename c383 Income
rename c381 Education
rename c130 Evangelical
recode Evangelical (2=0) (.=0)
replace Income =. if Income==99
gen linked =. 
replace linked = c151
replace linked =0 if c150==2
recode linked (0=0) (3=1) (2=2) (1=3)
gen vote_choice =.
replace vote_choice=1 if c14==1
replace vote_choice=0 if c14==2
// Recoding Variables to deal with missing values
replace Gender = . if Gender==3
recode Gender (2=1) (1=2)
/// Recoding Age to Decade
replace Age = Age/10
//Generating South variable
/// Non-South-0, South-1
generate South = 0
replace South = 1 if State=="AL"
replace South = 1 if State=="AR"
replace South = 1 if State=="FL"
replace South = 1 if State=="GA"
replace South = 1 if State=="LA"
replace South = 1 if State=="MS"
replace South = 1 if State=="NC"
replace South = 1 if State=="SC"
replace South = 1 if State=="TN"
replace South = 1 if State=="TX"
replace South = 1 if State=="VA"

// Generating PID variable
/// 5-strong R, 4-not strong R, 3-ind, 2-not strong D, 1-strong D
generate PID = .
order PID, before(c27)
replace PID = 1 if Party_ID==2 & Partisanship==1
replace PID = 2 if Party_ID==2 & Partisanship==2
replace PID = 3 if Party_ID==3 
replace PID = 4 if Party_ID==1 & Partisanship==2
replace PID = 5 if Party_ID==1 & Partisanship==1

//Generating Education variable
///H.S. or less-1, some college-2, B.A. and above-3
*generate Education = 0
*replace Education = 1 if c381<=3
*replace Education = 2 if c381==4
*replace Education = 3 if c381>=5

// Generating group dummy variables
generate Weekly_Church = 0
replace Weekly_Church = 1 if c131<=2
generate Catholic = 0
replace Catholic = 1 if Religion==1
generate Protestant = 0
replace Protestant = 1 if Religion==2
replace Protestant = 1 if Religion==3
generate Other_rel = 0
replace Other_rel = 1 if Religion>=4 & Religion<=6
gen No_Relig =0
replace No_Relig =1 if Religion>=7
replace Income= Income/2

//Polychoric 
polychoric Health_Care Tax_Cuts Gay_Marriage Immigration  [pw=weight]
polychoric Health_Care Tax_Cuts Gay_Marriage Immigration  if Latino==1
polychoric Health_Care Tax_Cuts Gay_Marriage Immigration  if Asian==1
polychoric Health_Care Tax_Cuts Gay_Marriage Immigration  if Black==1
polychoric Health_Care Tax_Cuts Gay_Marriage Immigration  if White==1

//factor analysis
factor Health_Care Tax_Cuts Gay_Marriage Immigration  
factor Health_Care Tax_Cuts Gay_Marriage Immigration  if Latino==1
factor Health_Care Tax_Cuts Gay_Marriage Immigration  if Asian==1
factor Health_Care Tax_Cuts Gay_Marriage Immigration  if Black==1
factor Health_Care Tax_Cuts Gay_Marriage Immigration  if White==1

// Polychoric correlations
polychoric Health_Care Tax_Cuts Clim_Change [pw=weight]
// Factor analysis 
display r(sum_w)
matrix poly1972 = r(R)
factormat poly1972, n(10146) factors(2) blanks(.4) ml
rotate
zscore Health_Care Tax_Cuts Clim_Change
gen econ = (z_Health_Care*.615)+(z_Tax_Cuts*.652)+(z_Clim_Change*.724)



// Summary stats for econ, soc, party ID for racial groups
fsum econ PID vote if Latino==1 [aw=weight], s(p50 sd min max)
fsum econ PID vote if Asian==1 [aw=weight], s(p50 sd min max)
fsum econ PID vote if Black==1 [aw=weight], s(p50 sd min max)
fsum econ PID vote if White==1 [aw=weight], s(p50 sd min max)

	
gen Race = 0
replace Race =1 if Black==1
replace Race =2 if Latino==1
replace Race =3 if Asian==1
label define Race 0 "White" 1 "Black" 2 "Latino" 3 "Asian" 
label values Race Race

gen Cuban =0
replace Cuban =1 if s10==6
gen Venz = 0
replace Venz =1 if s10==19
gen Viet =0 
replace Viet =1 if s8==6
gen BA=0
replace BA=1 if Education>=5

regress econ i.Race##c.PID i.Race##i.Gender i.Race##i.South i.Race##i.BA i.Race##c.Income i.Race##c.Age i.Race##i.Weekly_Church  i.Race##i.Catholic i.Race##i.Evangelical i.Race##i.No_Relig i.Race##c.linked [iw=weight]
outreg2 using CMPS, replace auto(2) dec(2) alpha(.05) symbol(*)

margins, dydx(PID) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF1, replace)
margins, dydx(Gender) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF2, replace)
margins, dydx(South) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF3, replace) 
margins, dydx(BA) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF4, replace) 
margins, dydx(Income) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF5, replace) 
margins, dydx(Age) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF6, replace) 
margins, dydx(Weekly_Church) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF7, replace) 
margins, dydx(Catholic) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF9, replace) 
margins, dydx(Evangelical) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF10, replace) 
margins, dydx(No_Relig) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF11, replace) 
margins, dydx(linked) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCEF12, replace) 

combomarginsplot fileCEF1 fileCEF2 fileCEF3 fileCEF4 fileCEF5 fileCEF6 fileCEF7  fileCEF9 fileCEF10 fileCEF11 fileCEF12, ///
	labels("PID" "Gender" "South" "BA" "Income" "Age" "Weekly Church"  "Catholic" "Evangelical" "No Religion" "Linked Fate") ///
	recast(scatter) x(_filenumber) horizontal ytitle("") xline(0, lc(red)) ///
	xtitle("Marginal effect of Group Memberships") title("Economic Attitudes") ///
	legend(pos(6) row(1)) xsize(8) ysize(6) xlabel(-1(.25).5) ///
	plot1opts(mcolor(white) mlcolor(black)) ci1opts(msize(0) col(black)) ///
	plot2opts(mcolor(gs10) mlcolor(black)) ci2opts(msize(0) col(black)) ///
	plot3opts(mcolor(black) mlcolor(black)) ci3opts(msize(0) col(black)) ///
	plot4opts(mcolor(gs5) mlcolor(black)) ci4opts(msize(0) col(black)) ///
	offset(0.2)	

graph save "CMPS Econ", replace 
graph export "CMPS Econ.pdf", replace 
	
regress PID i.Race##c.econ i.Race##i.Gender i.Race##i.South i.Race##i.BA i.Race##c.Income i.Race##c.Age i.Race##i.Weekly_Church   i.Race##i.Catholic i.Race##i.Evangelical i.Race##i.No_Relig i.Race##c.linked [iw=weight]
outreg2 using CMPS, append auto(2) dec(2) alpha(.05) symbol(*)

margins, dydx(econ) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF1, replace)
margins, dydx(Gender) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF2, replace)
margins, dydx(South) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF3, replace) 
margins, dydx(BA) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF4, replace) 
margins, dydx(Income) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF5, replace) 
margins, dydx(Age) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF6, replace) 
margins, dydx(Weekly_Church) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF7, replace)
margins, dydx(Catholic) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF9, replace) 
margins, dydx(Evangelical) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF10, replace) 
margins, dydx(No_Relig) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF11, replace) 
margins, dydx(linked) at(Race=(0 1 2 3)) predict(xb) level(90) saving(fileCPF12, replace) 

combomarginsplot fileCPF1 fileCPF2 fileCPF3 fileCPF4 fileCPF5 fileCPF6 fileCPF7 fileCPF9 fileCPF10 fileCPF11 fileCPF12, ///
	labels("Econ" "Gender" "South" "BA" "Income" "Age" "Weekly Church" "Catholic" "Evangelical" "No Religion"  "Linked Fate") ///
	recast(scatter) x(_filenumber) horizontal ytitle("") xline(0, lc(red)) ///
	xtitle("Marginal effect of Group Memberships") title("Party ID") ///
	legend(pos(6) row(1)) xsize(8) ysize(6) xlabel(-.5(.25).5) ///
	plot1opts(mcolor(white) mlcolor(black)) ci1opts(msize(0) col(black)) ///
	plot2opts(mcolor(gs10) mlcolor(black)) ci2opts(msize(0) col(black)) ///
	plot3opts(mcolor(black) mlcolor(black)) ci3opts(msize(0) col(black)) ///
	plot4opts(mcolor(gs5) mlcolor(black)) ci4opts(msize(0) col(black)) ///
	offset(0.2)	
graph save "CMPS PID", replace 
graph export "CMPS PID.pdf", replace 
	
logit vote i.Race##c.PID i.Race##c.econ i.Race##i.Gender i.Race##i.South i.Race##i.BA i.Race##c.Income i.Race##c.Age i.Race##i.Weekly_Church  i.Race##i.Catholic i.Race##i.Evangelical i.Race##i.No_Relig i.Race##c.linked [iw=weight]
outreg2 using CMPS, append auto(2) dec(2) alpha(.05) symbol(*)

margins, dydx(PID) at(Race=(0 1 2 3)) level(90) saving(fileCVF1, replace)
margins, dydx(econ) at(Race=(0 1 2 3)) level(90) saving(fileCVF2, replace)
margins, dydx(Gender) at(Race=(0 1 2 3))  level(90) saving(fileCVF3, replace)
margins, dydx(South) at(Race=(0 1 2 3)) level(90) saving(fileCVF4, replace) 
margins, dydx(BA) at(Race=(0 1 2 3)) level(90) saving(fileCVF5, replace) 
margins, dydx(Income) at(Race=(0 1 2 3))  level(90) saving(fileCVF6, replace) 
margins, dydx(Age) at(Race=(0 1 2 3))  level(90) saving(fileCVF7, replace) 
margins, dydx(Weekly_Church) at(Race=(0 1 2 3))  level(90) saving(fileCVF8, replace)
margins, dydx(Catholic) at(Race=(0 1 2 3)) level(90) saving(fileCVF10, replace) 
margins, dydx(Evangelical) at(Race=(0 1 2 3)) level(90) saving(fileCVF11, replace) 
margins, dydx(No_Relig) at(Race=(0 1 2 3)) level(90) saving(fileCVF12, replace) 
margins, dydx(linked) at(Race=(0 1 2 3)) level(90) saving(fileCVF13, replace) 

combomarginsplot fileCVF1 fileCVF2 fileCVF3 fileCVF4 fileCVF5 fileCVF6 fileCVF7 fileCVF8 fileCVF10 fileCVF11 fileCVF12 fileCVF13, ///
	labels("PID" "Econ" "Gender" "South" "BA" "Income" "Age" "Weekly Church"   "Catholic" "Evangelical" "No Religion"  "Linked Fate") ///
	recast(scatter) x(_filenumber) horizontal ytitle("") xline(0, lc(red)) ///
	xtitle("Marginal Effect of Group Memberships on Prob Dem Vote") title("Vote Choice") ///
	legend(pos(6) row(1)) xsize(8) ysize(6) xlabel(-.2(.05).15) ///
	plot1opts(mcolor(white) mlcolor(black)) ci1opts(msize(0) col(black)) ///
	plot2opts(mcolor(gs10) mlcolor(black)) ci2opts(msize(0) col(black)) ///
	plot3opts(mcolor(black) mlcolor(black)) ci3opts(msize(0) col(black)) ///
	plot4opts(mcolor(gs5) mlcolor(black)) ci4opts(msize(0) col(black)) ///
	offset(0.2)	

graph save "CMPS Vote Choice", replace
graph export "CMPS Vote Choice.pdf", replace 
